// Copyright 2006 Nokia Corporation.
//
// THIS SOURCE CODE IS PROVIDED 'AS IS', WITH NO WARRANTIES WHATSOEVER,
// EXPRESS OR IMPLIED, INCLUDING ANY WARRANTY OF MERCHANTABILITY, FITNESS
// FOR ANY PARTICULAR PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE
// OR TRADE PRACTICE, RELATING TO THE SOURCE CODE OR ANY WARRANTY OTHERWISE
// ARISING OUT OF ANY PROPOSAL, SPECIFICATION, OR SAMPLE AND WITH NO
// OBLIGATION OF NOKIA TO PROVIDE THE LICENSEE WITH ANY MAINTENANCE OR
// SUPPORT. FURTHERMORE, NOKIA MAKES NO WARRANTY THAT EXERCISE OF THE
// RIGHTS GRANTED HEREUNDER DOES NOT INFRINGE OR MAY NOT CAUSE INFRINGEMENT
// OF ANY PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS OWNED OR CONTROLLED
// BY THIRD PARTIES
//
// Furthermore, information provided in this source code is preliminary,
// and may be changed substantially prior to final release. Nokia Corporation
// retains the right to make changes to this source code at
// any time, without notice. This source code is provided for informational
// purposes only.
//
// Nokia and Nokia Connecting People are registered trademarks of Nokia
// Corporation.
// Java and all Java-based marks are trademarks or registered trademarks of
// Sun Microsystems, Inc.
// Other product and company names mentioned herein may be trademarks or
// trade names of their respective owners.
//
// A non-exclusive, non-transferable, worldwide, limited license is hereby
// granted to the Licensee to download, print, reproduce and modify the
// source code. The licensee has the right to market, sell, distribute and
// make available the source code in original or modified form only when
// incorporated into the programs developed by the Licensee. No other
// license, express or implied, by estoppel or otherwise, to any other
// intellectual property rights is granted herein.
package bcexchanger.ui;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Vector;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Gauge;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.List;

import bcexchanger.*;
import javax.microedition.io.file.FileConnection;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.StringItem;
import org.netbeans.microedition.lcdui.pda.FileBrowser;

/**
 * 
 * This class represents a screen displaying the list of contacts of
 * the address book. The user can either choose the one contact or
 * exit the application
 * 
 * This class peforms the reading of address book entries in the 
 * separate thread, because reading the long address book might
 * take significant time
 * 
 * The class is extending an example.BCExchanger.ui.Screen class and
 * implements Runnable interface.
 * 
 * @see example.BCExchanger.ui.Screen Design patterns: State
 */
public class SelectFileScreen extends Screen implements Runnable {

  private List list;
  private Command volverCmd;
  private Command exitCommand;
  private Command cmdBuscarArchivo;
  private Command cmdAgregarArchivo;

  private Gauge gauge;
  private Form progressBar;
  private Thread thread;
  private Command progressExitCommand;
    
  private Form formAgregar;
    private StringItem lbNombreArchivo;
    private StringItem lbTamanio;
    
    private Screen previa;
  
  /**
   * Constructor
   * 
   * @param _midlet -
   *          the parent class which keeps the current UI state
   */
  public SelectFileScreen(BCExchangerMIDlet _midlet, Screen previa) {
    super(_midlet);
   this.previa = previa;
    // create and add commands to ui list
    progressBar = new Form("Please wait");

    gauge = new Gauge("Reading contacts", false, Gauge.INDEFINITE, Gauge.CONTINUOUS_RUNNING);
    gauge.setLayout(Item.LAYOUT_CENTER);
    gauge.setLayout(Item.LAYOUT_VCENTER);
    progressBar.append(gauge);
    
    progressExitCommand = new Command("Exit", Command.EXIT, 1);
    progressBar.addCommand(progressExitCommand);

    progressBar.setCommandListener(this);

    displayable = progressBar;


    // start the thread reading the address book entries
    thread = new Thread(this);
    thread.start();
    
  }

  public void commandAction(Command command, Displayable _displayable) {

   midlet.changeScreen(new AlertMessage(midlet,
            "commandAction"+command.getLabel(), null));
    if (command == cmdBuscarArchivo) {
        midlet.changeScreen(new AlertMessage(midlet,
            "Mama", null));
        try 
        {
            midlet.changeScreen(new FilesScreen(midlet));
        }
        catch (Exception e1) 
        {
            midlet.changeScreen(new AlertMessage(midlet, "Cannot save own business card", null));
      }
    } 
    else if(command == cmdAgregarArchivo)
    {
        midlet.agregarArchivo();
        
        midlet.changeScreen(new AlertMessage(midlet,
            "Archivo agregado correctamente", this));
    }
    else if(command == volverCmd)
    {
        midlet.changeScreen(new ListFilesScreen(midlet));
    }
    else if (command == exitCommand || command == progressExitCommand) 
    {
      midlet.quit();
    } 
    else {
      // unknown command
      throw new RuntimeException("Internal error #20");
    }
  }

 
  
  /* (non-Javadoc)
   * @see java.lang.Runnable#run()
   */
  public void run() {
        try {
            // create and add commands to ui list
            displayable = getFormAgregar();
            getLbNombreArchivo().setText(midlet.getSelectedFile().getName());
            getLbTamanio().setText((midlet.getSelectedFile().fileSize()/1000)+" KBytes");
            midlet.changeScreen(this);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
	
    }
  
  public StringItem getLbNombreArchivo() {
        if (lbNombreArchivo == null) {                                 
            // write pre-init user code here
            lbNombreArchivo = new StringItem("Nombre Archivo", "");                                                                      
            // write post-init user code here
        }                         
        return lbNombreArchivo;
    }
  
  public StringItem getLbTamanio() {
        if (lbTamanio == null) {                                 
            // write pre-init user code here
            lbTamanio = new StringItem("Tamanio", "", Item.PLAIN);                                                                     
            // write post-init user code here
        }                         
        return lbTamanio;
    }
  
   public Form getFormAgregar() {
        if (formAgregar == null) {                                 
            // write pre-init user code here
            formAgregar = new Form("Archivo seleccionado", new Item[] { getLbNombreArchivo(), getLbTamanio() });                                                
            cmdBuscarArchivo = new Command("Buscar", Command.SCREEN, 0);                                   
            formAgregar.addCommand(cmdBuscarArchivo);
            cmdAgregarArchivo = new Command("Agregar Archivo", Command.SCREEN, 0);                                   
            formAgregar.addCommand(cmdAgregarArchivo);
            volverCmd = new Command("Volver", Command.SCREEN, 0);
            formAgregar.addCommand(volverCmd);
            exitCommand = new Command("Exit", Command.EXIT, 1);
            formAgregar.addCommand(exitCommand);
            formAgregar.setCommandListener(this);                                  
            // write post-init user code here
        }                         
        return formAgregar;
    }
  
}
